package com.att.action;

import com.adkfp.utils.LayuiUtil;
import com.att.bean.CheckExactBean;
import com.att.service.CheckExactService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("checkExact")
public class CheckExactAction {

    @Resource(name = "checkExactService")
    private CheckExactService checkExactService;

    /**
     * Description: 转发到页面
     */
    @RequestMapping("checkExactIndex")
    public String checkExactIndex() {
        return "check/checkExact";
    }

    //新增
    @RequestMapping("insert")
    @ResponseBody
    @Transactional
    public String insert(CheckExactBean bean) {
        bean.setCreateTime(new Date());
        bean.setIsDelete(0);
        bean.setIsAdd(1);
        bean.setIsModify(0);
        String[] idList = bean.getIds().split(",");
        for (int j = 0; j < idList.length; j++) {
            String id = idList[j];
            bean.setUserId(id);
            List<CheckExactBean> exactBeanList = checkExactService.findExactBeanList(bean);
            if (exactBeanList != null && exactBeanList.size() > 0) {
                for (int i = 0; i < exactBeanList.size(); i++) {
                    CheckExactBean exactBean = new CheckExactBean();
                    exactBean.setCheckTime(exactBeanList.get(i).getCheckTime());
                    exactBean.setCheckType(exactBeanList.get(i).getCheckType());
                    exactBean.setUserId(exactBeanList.get(i).getUserId());
                    exactBean.setYuanYin(exactBeanList.get(i).getYuanYin());
                    exactBean.setIsDelete(1);
                    exactBean.setIsAdd(0);
                    exactBean.setIsModify(0);
                    exactBean.setUserId(id);
                    exactBean.setCreateTime(new Date());
                    boolean flag = checkExactService.insert(exactBean);
                    if (!flag) {
                        return "ERROR";
                    }
                }
                boolean flag = checkExactService.insert(bean);
                if (!flag) {
                    return "ERROR";
                }
            } else {
                boolean flag = checkExactService.insert(bean);
                if (!flag) {
                    return "ERROR";
                }
            }
        }
        return "SUCCESS";
    }

    /**
     * Description: 考勤记录修改日志
     * @param bean
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll(CheckExactBean bean) {
        List<CheckExactBean> list = checkExactService.findByCondition(bean);
        for (int i = 0; i<list.size();i++){
            Integer add = list.get(i).getIsAdd();
            Integer upd = list.get(i).getIsModify();
            Integer del = list.get(i).getIsDelete();
            if(add != 0){
                list.get(i).setRunType(0);
            }else if (upd != 0){
                list.get(i).setRunType(1);
            }else if (del != 0){
                list.get(i).setRunType(2);
            }else {
                list.get(i).setRunType(99);
            }
        }
        long count = checkExactService.findByConditionCount(bean);
        return LayuiUtil.data(count, list);
    }
}
